<template>
    <div class="upd">
        <el-row class="head">
            <el-col >——修改维修人员/维修人员</el-col>
        </el-row>

        <el-row class="title">
            <el-col>修改维修人员信息</el-col>
        </el-row>
        <el-form  :model="form.admin" label-width="100px" class="formrule" :rules="rules" ref="ruleform">

            <el-form-item label="维修分类" prop="typeId">
                <el-select v-model="form.admin.typeId" placeholder="请选择"  >
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="账号" prop="username">
                <el-input v-model="form.admin.username"></el-input>
            </el-form-item>

            <el-form-item label="姓名" prop="name">
                <el-input v-model="form.admin.name"></el-input>
            </el-form-item>

            <el-form-item label="手机" prop="mobile">
                <el-input v-model="form.admin.mobile"></el-input>
            </el-form-item>

            <el-form-item label="部门" prop="company">
                <el-input v-model="form.admin.company"></el-input>
            </el-form-item>

            <el-form-item label="设为管理员">
                <el-switch v-model="form.delivery"></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit(form.admin)">新增</el-button>
                <el-button @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "upd",
        data(){
            return{
                admin: {
                    id: '',
                },
                form: {
                    admin:{
                        id:'',
                        typeId:'',
                        username:'',
                        password:'',
                        name:'',
                        mobile:'',
                        company:'',

                    },
                    delivery:false,
                },

                options:[],
                rules: {
                    typeId: [
                        {required: true, message: '请选择维修分类', trigger: 'change'}
                    ],
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
                    ],
                    mobile: [
                        {required: true, message: '请输入你的手机号码', trigger: 'blur'},
                        {min: 11, max: 11, message: '长度为11位的数字', trigger: 'blur'}
                    ],
                    company: [
                        {required: true, message: '请输入公司', trigger: 'blur'},
                        {min: 2, max: 18, message: '长度在 2 到 18 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        created()
        {

            this.$axios.post("/api/v1/user/seltypeall").then(result => {

                return result.data;
            })
                .then(data1 => {
                    this.options=data1;
                });
            this.admin.id=this.$route.params.id;
            this.$axios.post("/api/v1/user/super/seladminbyid",this.admin).then(result => {
                return result.data
            })
                .then(data =>{
                    this.form.admin=data.data;
                });
            console.log(this.form.admin);


        },
        methods:{
            onSubmit(admin){
                    this.$refs.ruleform.validate((valid) => {
                        if (valid) {
                            if (this.form.delivery)
                                admin.role=5;
                            else
                                admin.role=1;
                            this.$axios.post("/api/v1/user/super/updadmin",admin)
                                .then(result =>{
                                    return result.data
                                })
                                .then(data =>
                                {
                                    this.$message({
                                        type:'success',
                                        message:data.msg,
                                    });
                                    this.$router.push({
                                        path:`/user/list/`
                                    });
                                });
                        } else {
                            return false;
                        }
                    });


            },
            resetForm(){
                this.admin.typeId='';
                this.admin.delivery=false;
                this.admin.mobile='';
                this.admin.username='';
                this.admin.password='';
                this.admin.name='';
                this.admin.role=1;
                this.admin.company='';
            }
        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>